<template>
	<view class="good-list">
		<view class="good-list-item" v-for="(item, i) in goodList" :key="i" @click="jumpDetail(item)">
			<!-- <view class="img">
				<image :src="item.goods_small_logo"></image>
			</view>
			<view class="info">
				<text class="name">{{ item.goods_name }}</text>
				<text class="price">￥ {{ item.goods_price.toFixed(2) }}</text>
			</view> -->
			<my-goods :goods="item"></my-goods>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodList: [],
				total: 0,
				isloading: false // 节流阀
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodList()
		},
		methods: {
			async getGoodList(cb) {
				this.isloading = true
				const { data } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				this.isloading = false
				
				// 如果cb存在，就调用&&后的函数
				cb && cb()
				if(data.meta.status !== 200) return uni.$showMsg()
				// this.goodList = data.message.goods
				this.goodList = [...this.goodList, ...data.message.goods]
				this.total = data.message.total
				this.queryObj.pagenum = data.message.pagenum 
			},
			jumpDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			}
		},
		onReachBottom() {
			// 当数据加载到最后一页再无新数据时，就不触发继续下拉触底事件获取接口
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
			// 请求完毕isloading是false，没有请求完的时候不触发新的触底事件获取接口
			if(this.isloading) return
			// 上拉触底事件-上拉加载更多
			this.queryObj.pagenum++
			this.getGoodList()
		},
		onPullDownRefresh() {
			// 重置关键数据
			this.isloading = false
			this.queryObj.pagenum = 1
			this.goodList = []
			this.total = 0
			// 重新发起数据请求
			this.getGoodList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
.good-list {
	.good-list-item {
		display: flex;
		justify-content: space-between;
		padding: 10px;
		.img {
			margin-right: 10px;
			image {
				width: 100px;
				height: 100px;
			}
		}
		.info {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			width: 67%;
			.name {
				font-size: 12px;
			}
			.price {
				color: red;
			}
		}
	}
}
</style>
